﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using OAWeb.Models;

namespace OAWeb.Controllers
{
    public class DepartmentGradesController : Controller
    {
        private OADbContext db = new OADbContext();

        // GET: DepartmentGrades
        public ActionResult Index(int id)
        {
            var departmentGrades = db.DepartmentGrades.Include(d => d.Department).Where(d => d.DepartmentID == id);
            Session["did"] = id;
            return View(departmentGrades.ToList());
            
        }

        // GET: DepartmentGrades/Details/5
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            DepartmentGrade departmentGrade = db.DepartmentGrades.Find(id);
            if (departmentGrade == null)
            {
                return HttpNotFound();
            }
            return View(departmentGrade);
        }

        // GET: DepartmentGrades/Create
        public ActionResult Create()
        {
            ViewBag.DepartmentID = new SelectList(db.Departments, "DepartmentID", "DepartmentName");
            return View();
        }

        // POST: DepartmentGrades/Create
        // 为了防止“过多发布”攻击，请启用要绑定到的特定属性，有关 
        // 详细信息，请参阅 https://go.microsoft.com/fwlink/?LinkId=317598。
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "ID,Name,DepartmentID,CreatYear,Leader")] DepartmentGrade departmentGrade)
        {
            if (ModelState.IsValid)
            {
                db.DepartmentGrades.Add(departmentGrade);
                db.SaveChanges();
                return RedirectToAction("Index", new { id = departmentGrade.DepartmentID });
            }

            ViewBag.DepartmentID = new SelectList(db.Departments, "DepartmentID", "DepartmentName", departmentGrade.DepartmentID);
            return View(departmentGrade);
        }

        // GET: DepartmentGrades/Edit/5
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            DepartmentGrade departmentGrade = db.DepartmentGrades.Find(id);
            if (departmentGrade == null)
            {
                return HttpNotFound();
            }
            ViewBag.DepartmentID = new SelectList(db.Departments, "DepartmentID", "DepartmentName", departmentGrade.DepartmentID);
            return View(departmentGrade);
        }

        // POST: DepartmentGrades/Edit/5
        // 为了防止“过多发布”攻击，请启用要绑定到的特定属性，有关 
        // 详细信息，请参阅 https://go.microsoft.com/fwlink/?LinkId=317598。
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include = "ID,Name,DepartmentID,CreatYear,Leader")] DepartmentGrade departmentGrade)
        {
            if (ModelState.IsValid)
            {
                db.Entry(departmentGrade).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index", new { id = departmentGrade.DepartmentID });
            }
            ViewBag.DepartmentID = new SelectList(db.Departments, "DepartmentID", "DepartmentName", departmentGrade.DepartmentID);
            return View(departmentGrade);
        }

        // GET: DepartmentGrades/Delete/5
        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            DepartmentGrade departmentGrade = db.DepartmentGrades.Find(id);
            db.DepartmentGrades.Remove(departmentGrade);
            db.SaveChanges();
            return RedirectToAction("Index", new { id = departmentGrade.DepartmentID });
            if (departmentGrade == null)
            {
                return HttpNotFound();
            }
            return View(departmentGrade);
        }

        // POST: DepartmentGrades/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            DepartmentGrade departmentGrade = db.DepartmentGrades.Find(id);
            db.DepartmentGrades.Remove(departmentGrade);
            db.SaveChanges();
            return RedirectToAction("Index", new { id = departmentGrade.DepartmentID });
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
        //屏蔽错误提撒
        //public ActionResult Depart()
        //{
        //    return View();
        //}
    }
}
